﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Inspinia_MVC5.Data;
using Inspinia_MVC5.Models;
using Webdiyer.WebControls.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AdminAuthorize]
    public class TeachersController : Controller
    {
        private BooksSystemContext db = new BooksSystemContext();

        // GET: /Teachers/
        public ActionResult Index(string name, string idcard, int id = 1)
        {

            var teachers = db.Teachers.Include(t => t.Department);
            if (name != null)
            {
                teachers=teachers.Where(x=>x.TeaName.Contains(name));
            }
            if (idcard != null)
            {
                teachers=teachers.Where(x => x.TeaIdCard.Contains(idcard));
            }
            var model= teachers.OrderByDescending(a => a.ID).ToPagedList(id, 10);
            if (Request.IsAjaxRequest())
                return PartialView("_TeachersList", model);
            return View(model);
        }

        // GET: /Teachers/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Teacher teacher = db.Teachers.Find(id);
            if (teacher == null)
            {
                return HttpNotFound();
            }
            return View(teacher);
        }

        // GET: /Teachers/Create
        public ActionResult Create()
        {
            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DepartName");
            return View();
        }

        // POST: /Teachers/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="ID,TeaName,TeaJobNum,TeaIdCard,DepartmentId,TeaPwd,TeaPhone,Status")] Teacher teacher)
        {
            if (ModelState.IsValid)
            {
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase f = Request.Files["photo"];
                    if (f.ContentLength > 0)
                    {
                        string finename = @"/Content/TeachersPhoto/" + teacher.TeaIdCard + f.FileName.Substring(f.FileName.LastIndexOf('.'));
                        f.SaveAs(Server.MapPath(finename));
                        teacher.Photo = finename;
                    }
                    
                }
                teacher.Status = 1;
                db.Teachers.Add(teacher);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DepartName", teacher.DepartmentId);
            return View(teacher);
        }

        // GET: /Teachers/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Teacher teacher = db.Teachers.Find(id);
            if (teacher == null)
            {
                return HttpNotFound();
            }
            var select = new List<SelectListItem> { 
                new SelectListItem {Text="在职",Value="1" },
                new SelectListItem {Text="离职",Value="2" }
        };
            ViewBag.select = select;
            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DepartName", teacher.DepartmentId);
            return View(teacher);
        }

        // POST: /Teachers/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,TeaName,TeaJobNum,TeaIdCard,DepartmentId,TeaPwd,TeaPhone,Status")] Teacher teacher)
        {
            if (ModelState.IsValid)
            {
                db.Entry(teacher).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "DepartName", teacher.DepartmentId);
            return View(teacher);
        }

        // GET: /Teachers/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Teacher teacher = db.Teachers.Find(id);
            if (teacher == null)
            {
                return HttpNotFound();
            }
            return View(teacher);
        }

        // POST: /Teachers/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Teacher teacher = db.Teachers.Find(id);
            db.Teachers.Remove(teacher);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
